<?php
/**
 * Handles the uesr settings, including user interface integration.
 */
class ShoppSE_Settings
{	
	/**
	 * Container for details of setting pages and controllers. The format
	 * is an array of four values: label, slug, controller class name and the
	 * path to the controller.
	 * 
	 * @var array
	 */
	protected $available = array();
	
	
	
	/**
	 * Build list of admin pages and controllers.
	 * 
	 * @return ShoppSE_Settings
	 */
	public function __construct()
	{
		// Path to the controller directory
		$path = ShoppSE::$object->plugin_dir.'/inc/settings';
		
		// Subscriber page
		$this->available[] = array('Subscribers', 'subscribers', 'ShoppSE_Settings_Subscribers',
			"$path/subscribers.php");
			
		// Import list page
		$this->available[] = array('Import List', 'import', 'ShoppSE_Settings_Import',
			"$path/import.php");
			
		// Emails and notices page
		$this->available[] = array('Emails &amp; Notices', 'notices', 'ShoppSE_Settings_Notices',
			"$path/notices.php");
			
		// Related products page
		$this->available[] = array('Related Products', 'products', 'ShoppSE_Settings_Products',
			"$path/products.php");
			
		// Allow addition of new items
		$this->available = apply_filters('shoppse_settings_pages', $this->available);
	}
	
	
	/**
	 * Handles requests for the settings page.
	 */
	public function admin()
	{
		// Default controller and path
		$controller = $this->available[0][2];
		$controller_path = $this->available[0][3];
		
		// Has a specific subpage been requested?
		$subpage = array_key_exists('subpage', $_GET) ? $_GET['subpage'] : '';
		
		// Find controller/path for specified subpage
		foreach ($this->available as $option)
			if ($option[1] == $subpage)
			{
				$controller = $option[2];
				$controller_path = $option[3];
			}
		
		// Load and instantiate	
		require $controller_path;
		$controller = new $controller($this);
	}
	
	
	/**
	* Loads the specified view file (which will be automatically embedded within
	* the container view) and optionally passes the supplied array of key:value
	* pairs, which will be extracted and brought into scope.
	* 
	* @param mixed $file
	* @param mixed $vars
	*/
	public function view($file, array $vars = null)
	{
		// Path to the view files
		$viewpath = ShoppSE::$object->plugin_dir.'/inc/views/';
		
		// Load the inner view and capture output
		ob_start();
		
		// Extract and bring into scope any additional variables
		if ($vars !== null) extract($vars);
		
		// Include the view
		include $viewpath.$file.'.php';
		
		// Capture the output
		$page = ob_get_clean();
		
		// Prep the menu
		$menu = $this->derive_menu();
		
		// Wrap within the container view
		include $viewpath.'container.php';
	}
	
	
	/**
	 * Returns a list of menu labels and slugs.
	 * 
	 * @return array
	 */
	protected function derive_menu()
	{
		$menu = array();
		
		foreach ($this->available as $item)
			$menu[] = array_slice($item, 0, 2);
			
		return $menu;
	}
	
	
	/**
	 * Takes the supplied slug and returns a URL to access
	 * it.
	 * 
	 * @param string $slug
	 * @return string
	 */
	public static function setting_link($slug)
	{		
		$scheme = is_ssl() ? 'https://' : 'http://';
		$base = !empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
		$base .= $_SERVER['SCRIPT_NAME'];
		$query = '?'.http_build_query(array('page' => $_GET['page'], 'subpage' => $slug));
		
		return $scheme.$base.$query;
	}
}